package com.ceadeal.admin.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * SwaggerConfig
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Autowired
    private SwaggerPropsConfig spc;

    /**
     * 定义组
     * @return
     */
    @Bean
    public Docket defaultApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("default")
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .pathMapping("/")// base，最终调用接口后会和paths拼接在一起
                .select()
                .apis(RequestHandlerSelectors.basePackage(spc.getBasePackage()))
                .build()
                .apiInfo(defaultApiInfo())
                .enable(spc.getEnable());
    }

    /**
     * API信息
     * @return
     */
    private ApiInfo defaultApiInfo() {
        ApiInfo apiInfo = new ApiInfo(
                spc.getTitle(),              // 标题
                spc.getDescription(),        // 描述
                spc.getVersion(),            // 版本
                spc.getTermsOfServiceUrl(),  // Url的服务条款
                spc.getContactName(),        // 作者
                spc.getLicense(),            // 许可协议
                spc.getLicenseUrl()          // 许可协议Url
        );
        return apiInfo;
    }

}